<?php
namespace Anls\Com\Strategy;
use Anls\Com\Indicator;
/**
 * 策略
 * @author w.
 *
 */
class MaCross extends AbstractStrategySupport{
	
	// 最后出现交易信号回溯周期数过滤，默认5天
	private $filterLastSignalPeriod = 5;
	// 金死叉过滤，默认全过滤，'all,over,under'
	private $filterCrossType = 'all';

	// ma设置
	private $maPeriodShort = 5;
	private $maPeriodLong = 10;
	
	function __construct($bar,$symbolType,$code,$period){
	    $this->bar = $bar;
	    $this->symbolType = $symbolType;
	    $this->code = $code;
	    $this->period = $period;
    }
    
    private function initData(){
    	Indicator\Ma::calc($this->bar,$this->getMaPeriodShort());
		Indicator\Ma::calc($this->bar, $this->getMaPeriodLong());
    }
        
	public function test(){
		$this->initData();
		// 开始计算下标
		$begin = $this->getMaPeriodLong();
		// 最大下标
		$cnt = count($this->bar)-1;
		// 模拟交易
		for($i=$begin;$i<$cnt;$i++){
			$keyShort = Indicator\Ma::getKey($this->getMaPeriodShort());
			$keyLong = Indicator\Ma::getKey($this->getMaPeriodLong());
			// 判断是否上穿，金叉
			if($this->buyFlag == false && Indicator\Shape::crossOver($this->bar,$keyShort,$keyLong, $i)){
				dump($this->bar[$i]);
				exit();
				// 设置多仓交易数据
				$this->setBuyData($this->bar,$i);
			}
			// 多仓数据统计
			if($this->buyFlag == true){
				// 多仓开仓后数据统计
				$this->statsBuyOpenData($this->bar,$i);
				// 平仓条件
				$keyShort = Indicator\Ma::getKey($this->getMaPeriodShort());
				$keyLong = Indicator\Ma::getKey($this->getMaPeriodLong());
				// 平仓条件A：当前开盘价小于等于当前短周期均线价
				$cdtA = ($this->bar[$i]['open'] <= $this->bar[$i][$keyShort]);
				// 平仓条件B：当前收盘价小于等于当前长周期均线价
				$cdtB = ($this->bar[$i]['close'] <= $this->bar[$i][$keyLong]);
				if($cdtA && $cdtB){
					// 多仓平仓数据统计
					$this->statsBuyCloseData($this->bar,$i);
				}
			}
		}
		// 写入文件
		$this->writeTradeLog();
	}
	
	
	/**
	 * @return the $filterLastSignalPeriod
	 */
	public function getFilterLastSignalPeriod() {
		return $this->filterLastSignalPeriod;
	}

	/**
	 * @return the $filterCrossType
	 */
	public function getFilterCrossType() {
		return $this->filterCrossType;
	}

	/**
	 * @return the $maPeriodShort
	 */
	public function getMaPeriodShort() {
		return $this->maPeriodShort;
	}

	/**
	 * @return the $maPeriodLong
	 */
	public function getMaPeriodLong() {
		return $this->maPeriodLong;
	}

	/**
	 * @param number $filterLastSignalPeriod
	 */
	public function setFilterLastSignalPeriod($filterLastSignalPeriod) {
		$this->filterLastSignalPeriod = $filterLastSignalPeriod;
	}

	/**
	 * @param string $filterCrossType
	 */
	public function setFilterCrossType($filterCrossType) {
		$this->filterCrossType = $filterCrossType;
	}

	/**
	 * @param number $maPeriodShort
	 */
	public function setMaPeriodShort($maPeriodShort) {
		$this->maPeriodShort = $maPeriodShort;
	}

	/**
	 * @param number $maPeriodLong
	 */
	public function setMaPeriodLong($maPeriodLong) {
		$this->maPeriodLong = $maPeriodLong;
	}

	
	
}

?>
